package com.y.fund.service.mem;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.y.fund.aacore.dao.BaseDAO;
import com.y.fund.aacore.dao.HibernateParam;
import com.y.fund.aacore.data.PaginationList;
import com.y.fund.aacore.vo.PagingVo;
import com.y.fund.domain.mem.MB13UserInfo;
import com.y.fund.domain.mem.MB15LoginRecord;
import com.y.fund.utils.DateTimeUtil;
import com.y.fund.utils.StrUtil;
import com.y.fund.web.aavo.mem.LoginRecordSearchVo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("userService")
public class UserService{
	
	@Autowired
	BaseDAO baseDao;


	/** 分页查询人员信息列表
	 * @param pagingVo  分页信息
	 * @return PaginationList
	 */
	public PaginationList findUserList(MB13UserInfo user, String roleId, PagingVo pvo) {
		StringBuffer hql = new StringBuffer();
		List<HibernateParam> params = new ArrayList<HibernateParam>();

		// 初始化 HQL
		hql.append(" select u ")
				.append(" from ")
				.append(MB13UserInfo.class.getCanonicalName())
				.append(" u ");
		hql.append(" where 1=1 ");
		if(null != user) {
			if (StrUtil.NotBlankIgnoreNullStr(user.getCode())) {
				hql.append(" and u.code like :code ");
				params.add(new HibernateParam("code", "%" + user.getCode().trim() + "%"));
			}
			if (StrUtil.NotBlankIgnoreNullStr(user.getName())) {
				hql.append(" and u.name like :name ");
				params.add(new HibernateParam("name", "%" + user.getName().trim() + "%"));
			}
		}

		hql.append(" order by u.name asc ");

		return baseDao.findPaginatedList(hql.toString(), params, pvo, "u.id", true);
	}


	public PaginationList<MB15LoginRecord> findLoginRecordList(LoginRecordSearchVo searchVo, PagingVo pvo) {
		if(null == searchVo) {
			searchVo = new LoginRecordSearchVo();
		}
		StringBuffer hql = new StringBuffer();
		List<HibernateParam> params = new ArrayList<HibernateParam>();
		// 初始化 HQL
		hql.append(" from ").append(MB15LoginRecord.class.getCanonicalName()).append(" u where 1=1 ");
		
		if (StrUtil.NotBlankIgnoreNullStr(searchVo.getUserId())) {
			hql.append(" and u.userId=:userId ");
			params.add(new HibernateParam("userId", searchVo.getUserId()));
		}
		if (StrUtil.NotBlankIgnoreNullStr(searchVo.getUserName())) {
			hql.append(" and u.userName like :userName ");
			params.add(new HibernateParam("userName", "%" + searchVo.getUserName().trim() + "%"));
		}
		if (StrUtil.NotBlankIgnoreNullStr(searchVo.getUserCode())) {
			hql.append(" and u.userCode like :userCode ");
			params.add(new HibernateParam("userCode", "%" + searchVo.getUserCode().trim() + "%"));
		}
		if (StrUtil.NotBlankIgnoreNullStr(searchVo.getClientType())) {
			hql.append(" and u.clientType=:clientType ");
			params.add(new HibernateParam("clientType", searchVo.getClientType()));
		}
		if (null != searchVo.getLoginTimeStart()) {
			hql.append(" and u.loginTime>=:loginTimeStart ");
			params.add(new HibernateParam("loginTimeStart", searchVo.getLoginTimeStart()));
		}
		if (null != searchVo.getLoginTimeEnd()) {
			hql.append(" and u.loginTime<=:loginTimeEnd ");
			params.add(new HibernateParam("loginTimeEnd", searchVo.getLoginTimeEnd()));
		}
		if (null != searchVo.getStatus()) {
			hql.append(" and u.status=:status ");
			params.add(new HibernateParam("status", searchVo.getStatus()));
		}

		hql.append(" order by u.loginTime desc ");

		return baseDao.findPaginatedList(hql.toString(), params, pvo, "u.id", true);
	}
	

	/* 将除最新登录记录外的其他所有登录记录都失效 的 hql语句*/
	static final String LogoutRcdExcludeNewRecordHql = " update " + MB15LoginRecord.class.getName() + " lr set lr.status=-1, logoutTime=:logoutTime where status=1 and userId=:userId and id<>:excludeLgnRecordId ";
	/** 将除最新登录记录外的其他所有登录记录都失效，用于在用户登录成功后将此用户本次登录客户端类型的以前登录记录全部失效
	 * @param userId 登录用户ID， 必填
	 * @param excludeLgnRecordId 要排除的最新登录记录ID， 必填
	 *  */
	public void logoutRcdExcludeNewRecord(String userId, String clientType, String logoutTime, String excludeLgnRecordId) {
		if(StrUtil.isBlankIncNullStr(userId) || StrUtil.isBlankIncNullStr(excludeLgnRecordId)) {
			return ;
		}
		if(StrUtil.isBlankIncNullStr(logoutTime)) {
			logoutTime = DateTimeUtil.NowTime();
		}
		
		List<HibernateParam> params = new ArrayList<HibernateParam>();
		params.add(new HibernateParam("userId", userId));
		params.add(new HibernateParam("excludeLgnRecordId", excludeLgnRecordId));
		params.add(new HibernateParam("logoutTime", logoutTime));
		
		if(StrUtil.NotBlankIgnoreNullStr(clientType)) {
			params.add(new HibernateParam("clientType", clientType));
			baseDao.execUpdtHql((LogoutRcdExcludeNewRecordHql + " and clientType=:clientType "), params);
		}else {
			baseDao.execUpdtHql(LogoutRcdExcludeNewRecordHql, params);
		}
	}

}
